
/*
 *
 * Copyright (c) 2006 P.J.Leonard
 * 
 * http://www.frinika.com
 * 
 * This file is part of Frinika.
 * 
 * Frinika is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * Frinika is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Frinika; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package uk.ac.bath.machines.neural;

import java.io.StringWriter;
import uk.ac.bath.legacy.Operator;
import uk.ac.bath.legacy.VectorState;
import uk.ac.bath.util.MyRandom;

/**
 *
 * @author pjl
 */
class NeuralOperator extends Operator implements Cloneable {

    private int size;
    private double sum;
    private float w[];

    public NeuralOperator(int size) {
        this.size=size;
        w=new float[size];
    }

    @Override
    public void tick(VectorState in, VectorState out, int[] map) {
        sum=w[0];
        for (int i=1; i<size;i++) {
            sum+=in.v[map[i]].val*w[i];
        }
        out.v[map[0]].val=sigmoid(sum);
        
    }

    void randomize() {
        for(int i=0;i<size;i++) {
            w[i]=(float)(0.5-MyRandom.nextDouble());
        }
    }
    double sigmoid(double v){
      if (v> 0) return 1;
      else return 0;

    }

    @Override
    public int getStateSize() {
        return size;
    }

   
    public Object clone()  throws CloneNotSupportedException {
        NeuralOperator no=new NeuralOperator(size);
        System.arraycopy(w, 0, no.w, 0, size);
        return no;
    }

    @Override
    public void toString(StringWriter w) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
